<?php
class indexControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		if($_SERVER['HTTP_REFERER']=='' || preg_match("/m=login/",$_SERVER['HTTP_REFERER'])){
			$this->onMain();
		}else{
			$this->onList();
		}
	}
	
	
	
	public function onMain(){
		//今日订单
		$key="appymdian_index_".SHOPID;
		if(!$cache=cache()->get($key)){
			$neworder=M("ymdian_order")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND status=0 ",
				"fields"=>" count(order_id) as c"
			));
			$dayorder=M("stat_shoporder_day")->selectRow("shopid=".SHOPID." AND k='ymdian' AND kday='".date("Ymd")."' ");
			$monthorder=M("stat_shoporder_month")->selectRow("shopid=".SHOPID." AND k='ymdian' AND kmonth='".date("Ym")."' ");
			$ssnum=M("ymdian_order")->getOne("select count(distinct(sushe)) from ".table('ymdian_order')." WHERE shopid=".SHOPID." ");
			$alldakuan=M("ymdian_dakuan")->getOne("select sum(money) as money From ".table('ymdian_dakuan')." where shopid=".SHOPID." AND ispay=1 "); 
			$alldakuan2=M("stat_shoporder_day")->getOne("select sum(money) as money From ".table('stat_shoporder_day')." where shopid=".SHOPID." AND k='ymdian' AND jiesuan=0 ");
			$alldakuan+=$alldakuan2; 
			$allorder=M("kcorder")->getOne("select sum(money) as money from ".table('kcorder')."  where shopid=".SHOPID." AND typeid=1 AND (status=3 or isreceived=2) ");
			$allback =M("kcback")->getOne("select sum(money) as money from ".table('kcback')."  where shopid=".SHOPID." AND typeid=1 AND status<4 ");
			
			$allyakuan=$allorder-$allback;
			$wgnum=M("ymdian_weigui")->getOne("select count(*) from ".table('ymdian_weigui')." where shopid=".SHOPID." ");
			$wgdknum=M("ymdian_dakuan")->getOne("select count(*) from ".table('ymdian_dakuan')." where shopid=".SHOPID." AND iswg=1 ");
			$allmoney=M("stat_shoporder_month")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND k='ymdian' ",
				"fields"=>" sum(money) as money"
			));
			
			$allbalance=M("stat_shoporder_month")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND k='ymdian' ",
				"fields"=>" sum(balance) as money"
			));  
			$newuser=M("ymdian_user")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND kday='".date("Y-m-d")."' ",
				"fields"=>" count(*) as ct "
			));
			$alluser=M("ymdian_user")->selectOne(array(
				"where"=>" shopid=".SHOPID." ",
				"fields"=>" count(*) as ct "
			));
			$pros=M("ymdian_product")->select(array("where"=>" shopid=".SHOPID));			
			$prosmoney=0;
			$prosnum=0;
			if($pros){
				foreach($pros as $p){
					$ids[]=$p['productid'];
				}
				$ps=M("product")->getListByIds($ids);
				foreach($pros as $p){
					$prosmoney +=$p['total_num'] * $ps[$p['productid']]['price'];
					$prosnum+=$p['total_num'];
				}				 
			}
			 
			//未完成订单
			$unmoney=M("ymdian_order")->selectOne(array(
				"where"=>" shopid=".SHOPID." AND status<3",
				"fields"=>" sum(money) as money "
			));
			$prosmoney+=$unmoney;
			/*
			$kposmoney=0;
			$kposnum=0;
			$oids=M("kcorder")->selectCols(array("where"=>" shopid=".SHOPID." AND typeid=1 AND (status=3 or isreceived=2) ","fields"=>"order_id")); 
			if($oids){
				$kpos=M("kcorder_product")->select(array("where"=>"  order_id in("._implode($oids).")  ")); 
				if($kpos){
					foreach($kpos as $p){
						$ids[]=$p['productid'];
					}
					$ps=M("product")->getListByIds($ids);
					foreach($kpos as $p){
						$kposmoney +=$p['buy_num'] * $ps[$p['productid']]['price'];
						$kposnum+=$p['buy_num'];
					}
					 
				}
			}
			
			$oids=M("kcback")->selectCols(array("where"=>" shopid=".SHOPID." AND typeid=1 AND status=3 ","fields"=>"order_id")); 
			if($oids){
				$kpos=M("kcback_product")->select(array("where"=>" order_id in("._implode($oids).") ")); 
				if($kpos){
					 
					foreach($kpos as $p){
						$ids[]=$p['productid'];
					}
					$ps=M("product")->getListByIds($ids);
					foreach($kpos as $p){
						$kposmoney -=$p['buy_num'] * $ps[$p['productid']]['price'];
						$kposnum -=$p['buy_num'];
					}
					 
				}
			}
			echo $kposmoney." 产品数：".$prosnum." 库存数：".$kposnum;
			*/
			$allyouhui=M("ymdian_order")->getOne("select sum(discount_money) as money from ".table('ymdian_order')." where shopid=".SHOPID." AND status=3 ");
			$this->smarty->assign(array(
				"neworder"=>$neworder,
				"allbalance"=>$allbalance,
				"allmoney"=>$allmoney,
				"dayincome"=>$dayorder['money']*YM_GL_MMONEY,
				"dayorder"=>$dayorder,
				"monthorder"=>$monthorder,
				"sushe_num"=>$ssnum,
				"alldakuan"=>$alldakuan,
				"allyakuan"=>$allyakuan,
				"allyouhui"=>$allyouhui,
				"wgnum"=>$wgnum,
				"wgdknum"=>$wgdknum,
				"newuser"=>$newuser,
				"alluser"=>$alluser,
				"prosmoney"=>$prosmoney,
				"monthincome"=>$monthorder['money']*YM_GL_MMONEY
			));
			cache()->set($key,$this->smarty->_var,60); 
		 }else{
			$this->smarty->_var=$cache;
		} 
		$this->smarty->display("index.html");
		
	}
	
	public function onList(){
		$this->smarty->display("list.html");
	}
	
	public function onProduct(){
		$this->smarty->display("product.html");
	}
}
?>